package org.example.network.low.interfaces;

import java.net.*;
import java.util.Enumeration;
import java.util.List;

// 访问本机的网络接口（网卡）
public class NetworkInterfaceTests {
    public static void main(String[] args) throws SocketException {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            System.out.println("-----------------------------");
            System.out.println("网卡名称：" + networkInterface.getName());
            List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
            for (InterfaceAddress interfaceAddress : interfaceAddresses) {
                InetAddress inetAddress = interfaceAddress.getAddress();
                System.out.println("  地址：" + inetAddress.getHostAddress());
                System.out.println("     isLoopbackAddress:" + inetAddress.isLoopbackAddress());
                System.out.println("     isAnyLocalAddress:" + inetAddress.isAnyLocalAddress());
                System.out.println("     isLinkLocalAddress:" + inetAddress.isLinkLocalAddress());
                System.out.println("     isMCLinkLocal:" + inetAddress.isMCLinkLocal());
                System.out.println("     isMCNodeLocal:" + inetAddress.isMCNodeLocal());
                System.out.println("     isMulticastAddress:" + inetAddress.isMulticastAddress());
                System.out.println("     isSiteLocalAddress:" + inetAddress.isSiteLocalAddress()); // 站点本地地址（应该就是局域网地址的意思吧？）
                System.out.println("     inetAddress instanceof Inet4Address? :" + (inetAddress instanceof Inet4Address ? "true" : "false"));
                System.out.println("     inetAddress instanceof Inet6Address? :" + (inetAddress instanceof Inet6Address ? "true" : "false"));
            }
        }
    }
}
